<?php

class DBReader implements ArrayAccess {
  private $data;
	private $record;
	
	public function __construct($data){ $this->data = $data; }
  public function Read(){
    $row = mysql_fetch_array($this->data); 
    if (!$row) return false;
    $this->record = array();
    foreach ($row as $key=>$value) {
      $this->record[is_numeric($key) ? $key : strtolower($key)] = new DBValue($value);
		}
    return true;
  }
	public function Close(){ mysql_free_result($this->data); }

	public function GetRecord(){ return $this->record; }
  
	public function offsetExists($offset) {
		if ($this->record == null) throw new Exception('DBReader is not initialised');
		$key = is_numeric($offset) ? $offset : strtolower($offset);
		return isset($this->record[$key]);
	}
	public function offsetGet($offset) {
		if ($this->record == null) throw new Exception('DBReader is not initialised');
		$key = is_numeric($offset) ? $offset : strtolower($offset);
		if (!isset($this->record[$key])) throw new Exception('Field '.$offset.' not found.');
		return $this->record[$key];
	}
  public function offsetSet($offset, $value) {	throw new Exception('DBReader is readonly.'); }
	public function offsetUnset($offset) { throw new Exception('DBReader is readonly.'); }
}

?>